<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/3/14
 * Time: 17:33
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Membership extends Base
{
    /**
     * @return mixed|\think\response\Json
     * 会员列表
     */
    public function index()
    {
        if(Request::instance()->isAjax()){
            $params = input('param.');
            $where['user_auth'] = [['=', 2], ['=', 3], ['=', 4], 'or'];
            $where['user_del'] = 1;
            //会员类型
            if(isset($params['user_auth']) && $params['user_auth'] != ''){
                $where['user_auth'] = $params['user_auth'];
            }
            //用户名称
            if(isset($params['user_username']) && $params['user_username'] != ''){
                $where['user_username'] = ['like', '%' . $params['user_username'], '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('u_user')
                ->field('user_id, user_username, user_image, user_auth, user_auth_start, user_auth_end')
                ->where($where)
                ->page($Nowpage, $limits)
                ->order('user_id desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * @return mixed|\think\response\Json
     * 开通会员
     */
    public function addMember(){
        if(Request::instance()->isPost()){
            $user_id = input('user_id');
            $user_auth = input('user_auth');
            $start_time = input('start')?date('Y-m-d', strtotime(input('start'))):date('Y-m-d');
            if($user_auth == 2){//季度
                $rang_time = strtotime('3 month') - time();//时间戳的差值
            }else if($user_auth == 3){//半年
                $rang_time = strtotime('6 month') - time();
            }else{//一年
                $rang_time = strtotime('1 year') - time();
            }
            $time = strtotime($start_time) + $rang_time;
            $end_time = date('Y-m-d', $time);
            $update = [
                'user_auth' => $user_auth,
                'user_auth_start' => $start_time,
                'user_auth_end' => $end_time
            ];
            $res = Db::table('up_u_user')->where('user_id', $user_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '开通成功']);
            }else{
                return json(['code' => 100, 'msg' => '开通失败']);
            }
        }
        $user_auth_end = date('Y-m-d');
        $userLists = Db::table('up_u_user')
            ->where('user_status', 1)
            ->where('user_del', 1)
            ->where(function($query) use($user_auth_end){
                $query->where('user_auth', 1)->whereor('user_auth_end', '<', $user_auth_end);
            })
            ->field('user_id, user_username')
            ->select();
        $this->assign('userLists', $userLists);
        return $this->fetch('membership/addmember');
    }
    
    /**
     * @return mixed|\think\response\Json
     * 会员价格列表展示
     */
    public function memberPrice()
    {
        if(Request::instance()->isAjax()) {
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('member_price')
                ->count();// 获取总条数
            $lists = Db::name('member_price')
                ->field('mp_id, mp_auth, mp_price, mp_big_money, mp_phone, mp_ios_id, last_update_time')
                ->page($Nowpage, $limits)
                ->order('mp_id desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('membership/memberprice');
    }
    
    /**
     * @return mixed|\think\response\Json
     * 会员配置展示
     */
    public function memberSet()
    {
        if(Request::instance()->isAjax()) {
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('member_config')
                ->count();// 获取总条数
            $lists = Db::name('member_config')
                ->field('mc_id, mc_auth, mc_top, mc_fresh, mc_fabu, last_update_time')
                ->page($Nowpage, $limits)
                ->order('mc_id desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('membership/memberset');
    }
    
    /**
     * @return mixed|\think\response\Json
     * 编辑会员价格
     */
    public function memberPriceEdit()
    {
        if(Request::instance()->isPost()) {
            $id = input('mp_id');
            $update = [
                'mp_price' => input('mp_price'),
                'mp_big_money' => input('mp_big_money'),
                'last_update_time' => date('Y-m-d H:i:s')
            ];
            if(input('mp_phone') == 2){
                $update['mp_ios_id'] = input('mp_ios_id');
            }
            $res = Db::name('member_price')->where('mp_id', $id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $mp_id = input('id');
        $info = Db::name('member_price')
            ->field('mp_id, mp_price, mp_big_money, mp_phone, mp_ios_id')
            ->where('mp_id', $mp_id)
            ->find();
        $this->assign('info', $info);
        return $this->fetch('membership/memberpriceedit');
    }
    
    /**
     * @return mixed|\think\response\Json
     * 编辑会员配置
     */
    public function memberSetEdit()
    {
        if(Request::instance()->isPost()) {
            $id = input('mc_id');
            $update = [
                'mc_top' => input('mc_top'),
                'mc_fresh' => input('mc_fresh'),
                'mc_fabu' => input('mc_fabu'),
                'last_update_time' => date('Y-m-d H:i:s')
            ];
            
            $res = Db::name('member_config')->where('mc_id', $id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $mc_id = input('id');
        $info = Db::name('member_config')
            ->field('mc_id, mc_top, mc_fresh, mc_fabu')
            ->where('mc_id', $mc_id)
            ->find();
        $this->assign('info', $info);
        return $this->fetch('membership/membersetedit');
    }
}